package com.example.mysafegarud;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private long mTimeMillis;
    private GridView grid;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    /**
     * 这里引用了一个适配器
     * @link package.myAdatper#method(Context)
     */
    private void initView() {
        grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(new myAdatper(this));
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0://手机防盗
                        sp  = getSharedPreferences("ifSign",MODE_PRIVATE);
                        if(sp.getBoolean("sign",false)){
                            //弹出输入密码对话框
                            final GetDialog getDialog = new GetDialog(MainActivity.this);
                            getDialog.setCallBack(new GetDialog.MyCallBack() {
                                @Override
                                public void ok() {
                                    String mET = getDialog.mET.getText().toString().trim();
                                    String s = new SHA256Utils().SHA256Utils(mET);
                                    String s1 = sp.getString("passWord", null);
                                    if(s.equals(s1)){
                                        getDialog.dismiss();
                                        startActivity(new Intent(MainActivity.this,FActivity.class));
                                    }

                                }

                                @Override
                                public void cancle() {
                                    getDialog.dismiss();
                                }
                            });
                            getDialog.show();
                        }else {
                            //弹出设置密码对话框
                            final SetDialog setDialog = new SetDialog(MainActivity.this);
                            setDialog.setCallBack(new SetDialog.MyCallBack() {
                                @Override
                                public void ok() {
                                    String et = setDialog.mAffirmET.getText().toString().trim();
                                    String et1 = setDialog.mFirstPWDET.getText().toString().trim();
                                    if (et.equals(et1)){
                                        SharedPreferences.Editor edit = sp.edit();
                                        String s = new SHA256Utils().SHA256Utils(et);
                                        edit.putString("passWord",s);
                                        edit.putBoolean("sign",true);
                                        edit.apply();
                                        setDialog.dismiss();
                                    }else {
                                        Toast.makeText(MainActivity.this,"密码不一致",Toast.LENGTH_LONG).show();
                                    }
                                }
                                @Override
                                public void cancle() {
                                    setDialog.dismiss();
                                }
                            });
                            setDialog.show();
                        }
                        break;
                    case 1://通讯卫士

                        break;
                    case 2://软件管家

                        break;
                    case 3://病毒查杀

                        break;
                    case 4://缓存清理

                        break;
                    case 5://进程管理

                        break;
                    case 6://流量统计

                        break;
                    case 7://高级工具

                        break;
                    case 8://设置中心

                        break;
                }
            }
        });
    }

    /**
     * 开启新的Activity不关闭自己的
     * @param cls 新的Activity
     */
    private void toActivity(Class<?> cls){
        Intent intent = new Intent(MainActivity.this,cls);
        startActivity(intent);
    }

    /**
     * 按两次返回退出程序
     * @param keyCode  用户按下一个的一个键盘按键
     * @param event    包含全部键盘按键
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 当按下返回键时所执行的命令
        if (keyCode == event.KEYCODE_BACK) {
            //2秒内第二次按下返回键时
            if (System.currentTimeMillis() -mTimeMillis<2000) {
                //退出系统
                System.exit(0);
            } else {
                //第一次点击提醒
                Toast.makeText(this,"再按一次退出程序",Toast.LENGTH_LONG).show();
                //获取第一次点击时间
                mTimeMillis = System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}